package dao;

import java.util.List;

import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import entity.Employee;

@Stateless
public class EmployeeDao {
	@PersistenceContext
	EntityManager manager;
	
	public void createEmployee(Employee employee){
		manager.persist(employee);
		
	}
	//finds all the list of the employees
	public List<Employee> findAllEmployee(){
		String jpql="select e from Employee e";
		javax.persistence.TypedQuery<Employee> query = manager.createQuery(jpql, Employee.class);
		List<Employee> listOfEmployee = query.getResultList();
		return listOfEmployee;
	}
	
	//finds only one employee
	public Employee findEmployee(int id){
		Employee employee=manager.find(Employee.class, id);
		return employee;
		
	}
	//deletes one employee
	public int deleteEmployee(Employee employee){
		try {
			Employee deleteEmployee=manager.merge(employee);
			manager.remove(deleteEmployee);	
		}
		catch (Exception e){
			return -1;
		}
		return 0;
	}
	
	//updates an employee
	public void updateEmployee(Employee employee){
			Employee updateEmployee=manager.merge(employee);
	}
	
}
